package com.yoona.controller;

import com.yoona.common.BaseController;
import com.yoona.common.VResponse;
import com.yoona.entity.Student;
import com.yoona.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生Controller
 * @author YoonaDa
 * @version 2018-10-17
 */

@RestController
@RequestMapping(value = "/stu")
public class StudentController extends BaseController {

    @Autowired
    private StudentService studentService;


    /**
     * 用过ID查询信息
     * @param id
     * @return
     *
     * eg.http://localhost:8088/stu/findStudentById?id=5
     */
    @GetMapping("/findStudentById")
    public List<Student> findStudentById(int id){
        return studentService.findStudentById(id);
    }
    /**
     * 以一个List包含起来，返回的json包含在result中，显示为列表
     * @param student
     * @return
     *
     * http://127.0.0.1:8088/stu//findList
     *
     */
    @GetMapping(value = "/findList")
    public VResponse<List<Student>> getList(Student student){
        List<Student> list =studentService.findList(student);
        return VResponse.success(list);
    }
    /**
     * 根据id删除一条数据
     * @param student
     * @return
     */

    @PostMapping(value = "/delete")
    public VResponse<Object> deleteList(Student student){
        studentService.delete(student);
        return VResponse.success("删除成功");
    }

    /**
     * 数据回显，参数id
     * @param student
     * @return
     */

    @RequestMapping(value = "/formList",method = RequestMethod.POST)
         public VResponse<Object> formList(Student student){
         return VResponse.success((Object)student);
         }

    /**
     * 添加一整条数据
     * @param student
     * @param model
     * @return
     *
     * http://localhost:8088/stu/insertList
     */
    @PostMapping(value = "/insertList")
    public VResponse<Object> insertList(Student student,Model model){
        if (!beanValidator(model,student)){
            return formList(student);
        }
        studentService.insertList(student);
        return VResponse.success("增加成功");
    }


    /**
     * 通过id更新一条数据
     * @param student
     * @param model
     * @return
     * http://localhost:8088/stu/updateList
     */
    @PostMapping(value = "/updateList")
    public VResponse<Object> updateList(Student student,Model model){
        if (!beanValidator(model,student)){
            return formList(student);
        }
        studentService.updateList(student);
        return VResponse.success("更新成功");
    }




}
